//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// Merge two maps.
//

# include "StdAfx.h"
# include "AddMap.h"
# include "UndoRedo.h"
# include "Map.h"
# include "Types.h"
# include "Globals.h"

class CAddMapAction : public CEditAction {
  public:
	CAddMapAction (map_t & _mapToAdd) : m_mapToAdd (_mapToAdd) {}

	virtual void GetName (string_t & _str) const {_str.append ("Add map");}
	virtual cu_t CanUndo () const {return cuNo;}
	virtual bool Do ();
	virtual void Undo () {}
	virtual void Redo () {}

  private:
	map_t & m_mapToAdd;
};

bool CAddMapAction::Do () {
	CCritSec::CLock lock (g_cs);

	g_map.Join (m_mapToAdd);

	return true;
}

//////////////////////////////////////////////

bool AddMap (map_t & _mapToAdd, map_t & _map) {
	if (& _map == & g_map)
		return PerformEditAction (new CAddMapAction (_mapToAdd));
	else {
		CCritSec::CLock lock (g_cs);
		_map.Join (_mapToAdd);
		return true;
	}
}

bool CanAddMap (const map_t & _mapToAdd, const map_t & _map) {
	{
		CCritSec::CLock lock (g_cs);

		if (_map.pTypeSet == _mapToAdd.pTypeSet || _map.pTypeSet == & g_tsNull || _mapToAdd.pTypeSet == & g_tsNull)
			return true;
	}

	return YesNoMsgBox ("The merged data have different type set. Do you want to convert type set?");		
}
